import { gameBet } from '@/api/game';
import { checkForEmptyValues } from '@/utils/com'
const state = {
    //游戏名字判断
    gameName: '',
    //游戏信息以及金额限制的信息储存位置
    gameInfo: '',
    //单双还是奇数偶数
    gameType: '',
    //数字
    gameNuber: '',
    //金额
    credit: 0,
    //这个是八选三的选球数组编号
    ball83code: '',
    //这个是排位的选球数组编号
    ballpwcode: '',
    //是否投注
    isTouzhu: false,
    //是否游戏开始
    isgamehow: true,
     //控制高度状态
    tabHeight:false,
    activeIndex: 1,
    //游戏龙虎
    LongOrHu: '',
    //疯狂游戏龙虎
    fkLongOrHu: '',
    //游戏红绿灯参数
    hldData: '',
    //游戏类别id
    ruleId:'',
    chooseParam:'',
    //是否初始化
    isInit:false,
    conFigparam:{ruleId:'',betParam:''},
};
const mutations = {
    SET_PARAM:(state,data)=>{
        state.conFigparam = data;
    },
    //推出时重置房间状态
    INIT:(state)=>{
        state.conFigparam='';
        state.gameName = '';
        state.gameInfo = '';
        state.gameType = '';
        state.gameNuber = '';
        state.credit = 0;
        state.ball83code = '';
        state.ballpwcode = '';
        state.isTouzhu = false;
        state.isgamehow = true;
        state.tabHeight = false;
        state.activeIndex = 1;
        state.LongOrHu = '';
        state.fkLongOrHu = '';
        state.hldData = '';
    },
    //初始化游戏数据
    INITDATA:(state)=>{
        state.gameNuber = '';
        state.gameType = ''
        state.credit = 0;
        state.ball83code = '';
        state.ballpwcode = '';
        state.LongOrHu = '';
        state.fkLongOrHu = '';
        state.hldData = '';
        state.conFigparam.ruleId = '';
        state.conFigparam.betParam = '';
    },
    //设置提交状态来判断界面是否更新
    INTE_PAGE_STATE: (state, isInit) => {
        state.isInit = isInit;
    },
    //设置游戏名字
    SET_GAME_NAME: (state, gameName) => {
        state.gameName = gameName;
    },
    //设置游戏信息
    SET_GAME_INFO: (state, gameInfo) => {
        state.gameInfo = gameInfo;
    },
    //设置游戏类型
    SET_GAME_TYPE: (state, gameType) => {
        state.gameType = gameType;
    },
    //设置单选数字
    SET_GAME_NUBER: (state, gameNuber) => {
        state.gameNuber = gameNuber;
    },
    //设置8选3数字
    SET_GAME_83NUBER: (state, ball83code) => {
        state.ball83code = ball83code;
    },
    REMOVE_83NUBER: (state) => {
        state.ball83code = '';
    },
    //设置排位数字
    SET_GAME_PWNUBER: (state, ballpwcode) => {
        state.ballpwcode = ballpwcode;
    },
    REMOVE_PWNUBER: (state) => {
        state.ballpwcode = '';
    },
    //设置钱
    SET_GAME_MONEY: (state, credit) => {
        state.credit = credit;
    },
    // 清除游戏数据
    REMOVE_GAME_DATA: (state) => {
        state.credit = '';
        state.ball83code = '';
        state.ballpwcode = '';
        state.gameNuber = '';

    },
    //控制界面的展示
    SET_isTouzhu: (state, isTouzhu) => {
        state.isTouzhu = isTouzhu;
    },
    //控制界面的展示
    SET_tabHeight: (state, tabHeight) => {
        state.tabHeight = tabHeight;
    },
    SET_isgamehow: (state, isgamehow) => {
        state.isgamehow = isgamehow;
    },
    SET_activeIndex: (state, activeIndex) => {
        state.activeIndex = activeIndex;
    },
    //设置龙虎的参数0，1
    SET_LONGHU: (state, LongOrHu) => {
        state.LongOrHu = LongOrHu;
    },
    //设置疯狂龙虎的参数0，1
    SET_FKLONGHU: (state, fkLongOrHu) => {
        state.fkLongOrHu = fkLongOrHu;
    },
    //设置红绿灯的游戏参数0，1
    SET_HLD: (state, hldData) => {
        state.hldData = hldData;
    },
    SET_CHOOSE_PARAM(state, chooseParam) {
        state.chooseParam = chooseParam;
      },
      SET_RULE_ID(state, ruleId) {
        state.ruleId = ruleId;
      },
};

const actions = {
    async handleGame({ commit }, gameName) {
        commit('SET_GAME_NAME', gameName);
    },
    async handleGameInfo({ commit }, gameInfo) {
        var gameName = ''
        if (gameInfo.id === 1) {
            gameName = 'gyjj_gj'
        }
        if (gameInfo.id === 2) {
            gameName = 'sdlh'
        }
        if (gameInfo.id === 3) {
            gameName = 'fklh'
        }
        if (gameInfo.id === 4) {
            gameName = 'hld'
        }
        commit('SET_GAME_NAME', gameName);
        commit('SET_GAME_INFO', gameInfo);
    },
    async setGameType({ commit }, gameType) {
        commit('SET_GAME_TYPE', gameType);
    },
    async setMoney({ commit }, credit) {
        commit('SET_GAME_MONEY', credit);
    },
    async removeData({ commit }) {
        commit('REMOVE_GAME_DATA');
    },
    async setGameNuber({ commit }, gameNuber) {
        var newgameNumber = gameNuber + 1
        commit('SET_GAME_NUBER', newgameNumber);
    },
    //设置八选三的游戏数字
    async setSevenGameNuber({ commit }, value) {
        commit('SET_GAME_83NUBER', value);
    },
    async resetGame83({ commit }) {
        commit('REMOVE_83NUBER');
    },
    //设置排位的游戏数字
    async setPwGameNuber({ commit }, value) {
        commit('SET_GAME_PWNUBER', value);
    },
    async resetGamePw({ commit }) {
        commit('REMOVE_PWNUBER');
    },
    //控制界面显示
    async setIsGameShow({ commit }, value) {
        commit('SET_isgamehow', value);
    },
    async setIsTouzhu({ commit }, value) {
        commit('SET_isTouzhu', value);
    },
    async setActiveIndex({ commit }, value) {
        commit('SET_activeIndex', value);
    },
    async setTabHeight({ commit }, value) {
        commit('SET_tabHeight', value);
    },
    //设置龙虎的参数
    async setLongHu({ commit }, LongOrHu) {
        commit('SET_LONGHU', LongOrHu);
    },
    //设置九龙虎的参数
    async set9LongHu({ commit }, fkLongOrHu) {
        commit('SET_FKLONGHU', fkLongOrHu);
    },
    //设置红绿灯的参数
    async setHld({ commit }, hldData) {
        commit('SET_HLD', hldData);
        console.log('hldData',hldData);
    },
    async ruleIdFunc({state,commit}){
        return new Promise((resolve)=>{
            let chooseParam = {};
            var ruleId=''
            if (state.gameName === 'gyjj_gj') {
                //冠军/游戏类型为偶数
                if (state.gameType === 'ball') {
                    chooseParam = {
                        ruleId: "TOP1_NO",
                        betParam: String(state.gameNuber)// 使用 state 中的 gameNuber
                    }
                      ruleId='TOP1_NO'
                }
                if (state.gameType === 'even') {
                    chooseParam = {
                        ruleId: "TOP1_DAN",
                        betParam: '-1'
                    }
                      ruleId='TOP1_DAN'
                }
                if (state.gameType === 'odd') {
                    chooseParam = {
                        ruleId: "TOP1_SHUANG",
                        betParam: '-1'
                    }
                      ruleId='TOP1_SHUANG'
                }
                if (state.gameType === 'big') {
                    chooseParam = {
                        ruleId: "TOP1_DA",
                        betParam: '-1'
                    }
                      ruleId='TOP1_DA'
                }
                if (state.gameType === 'small') {
                    chooseParam = {
                        ruleId: "TOP1_XIAO",
                        betParam: '-1'
                    }
                     ruleId='TOP1_XIAO'
                }
            }
            if (state.gameName === 'gyjj_yj') {
                //亚军/游戏类型为偶数
                if (state.gameType === 'ball') {
                    chooseParam = {
                        ruleId: "TOP2_NO",
                        betParam: String(state.gameNuber)// 使用 state 中的 gameNuber
                    }
                     ruleId='TOP2_NO'
                }
                if (state.gameType === 'even') {
                    chooseParam = {
                        ruleId: "TOP2_DAN",
                        betParam: '-1'
                    }
                    ruleId='TOP2_DAN'
                }
                if (state.gameType === 'odd') {
                    chooseParam = {
                        ruleId: "TOP2_SHUANG",
                        betParam: '-1'
                    }
                    ruleId='TOP2_SHUANG'
                }
                if (state.gameType === 'big') {
                    chooseParam = {
                        ruleId: "TOP2_DA",
                        betParam: '-1'
                    }
                    ruleId='TOP2_DA'
                }
                if (state.gameType === 'small') {
                    chooseParam = {
                        ruleId: "TOP2_XIAO",
                        betParam: '-1'
                    }
                    ruleId='TOP2_XIAO'
                }
            }
            if (state.gameName === 'gyjj_jj') {
                //季军/游戏类型为偶数
                if (state.gameType === 'ball') {
                    chooseParam = {
                        ruleId: "TOP3_NO",
                        betParam: String(state.gameNuber)// 使用 state 中的 gameNuber
                    }
                    ruleId='TOP3_NO'
                }
                if (state.gameType === 'even') {
                    chooseParam = {
                        ruleId: "TOP3_DAN",
                        betParam: '-1'
                    }
                    ruleId='TOP3_DAN'
                }
                if (state.gameType === 'odd') {
                    chooseParam = {
                        ruleId: "TOP3_SHUANG",
                        betParam: '-1'
                    }
                    ruleId='TOP3_SHUANG'
                }
                if (state.gameType === 'big') {
                    chooseParam = {
                        ruleId: "TOP3_DA",
                        betParam: '-1'
                    }
                    ruleId='TOP3_DA'
                }
                if (state.gameType === 'small') {
                    chooseParam = {
                        ruleId: "TOP3_XIAO",
                        betParam: '-1'
                    }
                    ruleId='TOP3_XIAO'
                }
            }
            if (state.gameName === 'seven') {
                //游戏八选三
                chooseParam = {
                    ruleId: "SELECT83",
                    betParam: state.ball83code
                }
                ruleId='SELECT83'
            }
            if (state.gameName === 'dzpw') {
                //排位
                chooseParam = {
                    ruleId: "RANKING",
                    betParam: state.ballpwcode
                }
                ruleId='RANKING'
            }
            if (state.gameName === 'sdlh') {
                if (state.LongOrHu === 1) {
                    chooseParam = {
                        ruleId: "TOP1_LONG",
                        betParam: '-1'
                    }
                    ruleId='TOP1_LONG'
                } else {
                    chooseParam = {
                        ruleId: "TOP1_HU",
                        betParam: '-1'
                    }
                    ruleId='TOP1_HU'
                }
            }
            if (state.gameName === 'fklh') {
                chooseParam = {
                    ruleId: state.fkLongOrHu.id,
                    betParam: '-1'
                }
                ruleId=state.fkLongOrHu.id
            }
            if (state.gameName === 'hld') {
                console.log('state.hldData.id',state.hldData.id );
                if(state.hldData.id === 'TOP1_NO'){
                    chooseParam = {
                        ruleId: 'TOP1_NO',
                        betParam:  String(state.gameNuber)
                    }
                }else{
                    chooseParam = {
                        ruleId: state.hldData.id,
                        betParam: '-1'
                    }
                  }
                ruleId=state.hldData.id
            }
            commit('SET_CHOOSE_PARAM', chooseParam);
            commit('SET_RULE_ID', ruleId);
            resolve({chooseParam,ruleId})
        })
    
    },
    async submit({ state ,dispatch,commit}) {
        //先匹配参数是否正确，然后匹配房间金额//然后提交接口
        await dispatch('ruleIdFunc');
        var chooseParam =state.chooseParam
        //合并参数然后找到这个roleId然后根据这个id找到投放限制的规则
        let param = {
            ...chooseParam,
            roomUuid: state.gameInfo.uuid,
            credit: state.credit,
        }
        return new Promise((resolve, reject) => {
            const emptyFields = checkForEmptyValues(param);
            if (emptyFields.length > 0) {
                //判断提交上来的有没有错误信息
                reject(emptyFields)
            } else {
                if(param.betParam==="-1"){
                    param.betParam=''
                }
                //提交前一步把这个修改一下
                gameBet(param).then(res => {
                    if(res.data.code==='200'){
                        commit('SET_PARAM',param)
                    }
                    resolve(res)
                })
            }
        });
    },

};
export default {
    namespaced: true,
    state,
    mutations,
    actions
};
